#include <stdio.h>
int main()
{
	int n;
	scanf("%d",&n);
	getchar();
	char s[1000];
	for(int i=0;i<n;i++){
		int cnt1=0,cnt2=0,cnt3=0,cntA=0,cntP=0,cntT=0,m=0,t=0;
		gets(s);
		int j=0;
		while(s[j] != '\0'){
			if(s[j]=='A'){
				cntA++;
			}else if(s[j]=='P'){
				cntP++;
			}else if(s[j]=='T'){
				cntT++;
			}
			if(s[j]!='A'&&s[j]!='P'&&s[j]!='T'){
				printf("NO\n");
				m++;
				break;
			} 
			j++;
		}
		    j=0;
		while(s[j]!='P'&&s[j]!='\0'){
			cnt1++;
			j++;
		}
		while(s[j]!='T'&&s[j]!='\0'){
			cnt2++;
			j++;
		}   
		cnt2-=1;                         
		while(s[j]!='\0'){
			cnt3++;
			j++;
		}
		cnt3-=1;
		if((cntA==0||cntP!=1||cntT!=1)&&m==0){
			t++;
			printf("NO\n");
		}
		if(cnt1*cnt2!=cnt3&&m==0&&t==0){
			printf("NO\n");
		}
		if(cnt1*cnt2==cnt3&&t==0&&m==0&&cnt2!=0){
			printf("YES\n");
		}else if(cnt1*cnt2==cnt3&&t==0&&m==0&&cnt2==0){
		printf("NO\n");
		}		
	}
	return 0;
 } 
